package com.yml.mybatis.gererator.meta;

import com.yml.mybatis.gererator.util.StrUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 列信息
 *
 * @author YML
 * @since 2023-07-20 09:14
 */
public class ColumnInfo {

    /** 表ID **/
    private Long tableId;
    /** 列ID **/
    private Long id;
    /** 列名 **/
    private String name;
    /** 列类型 **/
    private String type;
    /** 列说明 **/
    private String comment;
    /** 是否可为空 Y:不可为空，N:可为空 **/
    private String notNull;
    /** 默认值 **/
    private String defaultValue;
    /** java类型 **/
    private String javaType;
    /** java类型 **/
    private String javaFiled;
    /** 主键 1 ：是 0 ：不是 **/
    private String primaryKey;

    public ColumnInfo(Long tableId,Long id, String name, String type) {
        this.id = id;
        this.tableId = tableId;
        this.name = name;
        this.type = type;
        this.javaFiled = StrUtils.camelCase(name,false);
        this.javaType = generatorJavaType();
    }

    private String generatorJavaType(){
        if (StringUtils.equalsAnyIgnoreCase(type,"char","varchar","nvarchar","text","longtext")){
            return "String";
        }
        else if (StringUtils.equalsAnyIgnoreCase(type,"datetime","datetime2")){
            return "LocalDateTime";
        }
        else if (StringUtils.equalsAnyIgnoreCase(type,"date")){
            return "LocalDate";
        }
        else if (StringUtils.equalsAnyIgnoreCase(type,"int","tinyint","smallint","mediumint")){
            return "Integer";
        }
        else if (StringUtils.equalsAnyIgnoreCase(type,"bigint","long")){
            return "Long";
        }
        else if (StringUtils.equalsAnyIgnoreCase(type,"decimal")){
            return "BigDecimal";
        }
        else {
            return "Other";
        }
    }

    public String getJavaType() {
        return javaType;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        if (StringUtils.isBlank(comment)){
            this.comment = "";
            return;
        }
        this.comment = comment;
    }

    public Long getTableId() {
        return tableId;
    }

    public void setTableId(Long tableId) {
        this.tableId = tableId;
    }

    public String getNotNull() {
        return notNull;
    }

    public void setNotNull(String notNull) {
        this.notNull = notNull;
    }

    public String getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(String defaultValue) {
        if (StringUtils.isBlank(defaultValue)){
            this.defaultValue = "";
            return;
        }
        this.defaultValue = defaultValue;
    }

    public String getJavaFiled() {
        return javaFiled;
    }

    public String getPrimaryKey() {
        return primaryKey;
    }

    public void setPrimaryKey(String primaryKey) {
        this.primaryKey = primaryKey;
        if (!StringUtils.equalsAnyIgnoreCase("1",this.primaryKey)){
            this.primaryKey = "0";
        }
    }

    public void setJavaType(String javaType) {
        this.javaType = javaType;
    }

    public void setJavaFiled(String javaFiled) {
        this.javaFiled = javaFiled;
    }
}
